import {createService} from '@/request/service';
import {isEmpty, openModal} from '@/utils';
import UserManualSelector from '@/components/UserManualSelector/UserManualSelector.vue';
import {notification} from 'ant-design-vue';
import FlowUserSignModal from '@/components/flow/FlowUserSignModal/FlowUserSignModal.vue';
import FlowPreviewModal from '@/components/flow/FlowPreviewModal/FlowPreviewModal.vue';
import {createEmptyUserTask} from '@/components/flow/typs/flow';
import SubtractUserSignModal from '@/components/flow/SubtractUserSignModal/SubtractUserSignModal.vue';
import WidthdrawModal from '@/components/WidthdrawModal/WidthdrawModal.vue';

export default function ({baseURL, headers}) {
  let request = {baseURL, headers};
  let service = createService(request);
  return {
    preStart(params, callback = (userManualSelect) => ({})) {
      let {formModel, formTypeId, formId} = params;
      service.post('/flow/preStartProcess', params).then(userManualSelect => {
        formModel = formModel || {};
        formModel.userManualSelect = userManualSelect;
        formModel.id = formId;

        if (userManualSelect && !isEmpty(userManualSelect.select)) {
          openModal(UserManualSelector, {userManualSelect: userManualSelect}, userManualSelect => {
            formModel.userManualSelect = userManualSelect;
            callback && callback(formModel);
          });
          return;
        }
        callback && callback(formModel);
      });
    },
    complete({flowUserTaskId, formModel, userTaskComment}, callback = () => ({})) {
      //选择下个审批人
      service.get('/flow/getFlowUserTaskById', {params: {flowUserTaskId}}).then(flowUserTask => {
        if (isEmpty(flowUserTask)) {
          notification.warn({message: '没有找到审批任务, 请刷新页面重试'});
          return;
        }
        let data = {flowUserTaskId, ...userTaskComment};
        // this.resolveUserSign(data, flowUserTask, () => {
          this.preComplete({flowUserTaskId, formModel, userTaskComment}, userManualSelect => {
            data.userManualSelect = userManualSelect;
            data.approveType = 'approve';
            service.post('/flow/complete', data).then(() => {
              callback && callback();
            });
          });
        });
      // });
    },
    resolveUserSign(data, flowUserTask, callback = () => ({})) {
      if (flowUserTask.mustSignature && !data.userSign) {
        openModal(FlowUserSignModal, {value: data}, () => callback && callback());
      } else {
        callback && callback();
      }
    },
    preComplete({flowUserTaskId, formModel, userTaskComment}, callback = () => ({})) {
      service.post('/flow/precomplete', {formModel, flowUserTaskId}).then(data => {
        if (data && !isEmpty(data.select)) {
          openModal(UserManualSelector, {userManualSelect: data}, userManualSelect => {
            callback && callback(userManualSelect);
          });
          return;
        }
        callback && callback(null);
      });
    },
    showProcessHistory(processInstanceId) {
      openModal(FlowPreviewModal, {processInstanceId, request});
    },
    end({flowUserTaskId, comment}, callback = () => ({})) {
      service.post('/flow/end', {flowUserTaskId, comment, approveType: 'end'}).then(data => callback && callback());
    },
    toForm({flowUserTaskId, comment}, callback = () => ({})) {
      service.post('/flow/toForm', {
        flowUserTaskId,
        comment,
        approveType: 'toForm'
      }).then(data => callback && callback());
    },
    notify({UserTaskConfigModal, flowUserTaskId}, callback = () => ({})) {
      let userTask = createNotifyUserTask();
      openModal(UserTaskConfigModal, {userTask}, temp => {
        userTask = temp || userTask;
        let userConfigsStr = userTask.userConfigs;
        if (userTask && userTask.userConfigs) {
          try {
            userTask.userConfigs = JSON.parse(userTask.userConfigs);
          } catch (e) {
          }
        }
        service.post('/flow/queryNotifyFlowUser', {flowUserTaskId, userTask}).then(flowUsers => {
          if (!flowUsers || flowUsers.length <= 0) {
            notification.warn({message: '没有找到可知会的用户'});
            return;
          }
          service.post('/flow/addNotify', {
            flowUsers,
            flowUserTaskId,
            commentInfo: userConfigsStr
          }).then(data => callback && callback());
        });
      });
    },
    countUserSign({UserTaskConfigModal, flowUserTaskId}, callback) {
      let userTask = createEmptyUserTask();
      userTask.addCountSign = true;
      openModal(UserTaskConfigModal, {userTask, countUserSign: true}, temp => {
        userTask = temp || userTask;
        if (userTask.userConfigs) {
          userTask.userConfigs = JSON.parse(userTask.userConfigs);
        }
        notification.success({message: '加签成功'});
        callback = callback || (() => ({}));
        service.post('/flow/countUserSign', {flowUserTaskId, userTask}).then(callback);
      });
    },
    subtractUserSign({flowUserTaskId}, callback) {
      service.get('/flow/querySubtractAbleUserSign',
        {params: {flowUserTaskId}}).then(userTasks => {
        if (!userTasks || !userTasks.length) {
          notification.warn({message: '当前没有可以减签的流程节点'});
          return;
        }
        openModal(SubtractUserSignModal, {userTasks}, userTaskId => {
          notification.success({message: '减签成功'});
          callback = callback || (() => ({}));
          service.post('/flow/subtractAbleUserSign', {flowUserTaskId, userTaskId}).then(callback);
        });
      });
    },
    widthdraw({processInstanceId}, callback = () => ({})) {
      openModal(WidthdrawModal, {}, comment => {
        service.post('/flow/widthdraw', {processInstanceId, comment}).then(callback)
      })
    }
  };
}

function createNotifyUserTask() {
  let res = createEmptyUserTask();
  res.taskName = '知会临时任务';
  return res;
}